package cn.information.view.Tj;

import cn.information.dao.ProductDao;
import cn.information.utils.DButil;
import cn.information.view.MainUI;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.sql.*;
import java.util.Vector;

public class ProductSalesTjUI extends JFrame {
    private JScrollPane jScrollPane = new JScrollPane();
    private AbstractTableModel tableModel;
    private Vector<Vector<Object>> vector = new Vector<>();

    public ProductSalesTjUI() {
        setTitle("商品销售总量统计");
        setSize(800, 400);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        JPanel panelCenter = new JPanel();
        JPanel panelSouth = new JPanel();

        panelCenter.add(jScrollPane);
        createTable();

        add(panelCenter, BorderLayout.CENTER);
        add(panelSouth, BorderLayout.SOUTH);

        JButton btnBack = new JButton("返回");
        btnBack.addActionListener(e -> {
            dispose();
            new MainUI(null).setVisible(true);
        });
        panelSouth.add(btnBack);

        JButton btnExit = new JButton("退出");
        btnExit.addActionListener(e -> dispose());
        panelSouth.add(btnExit);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void createTable() {
        String[] columnNames = {"商品ID", "商品名称", "销售总量", "销售总价(元)"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);

        loadSalesData();

        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return vector.get(rowIndex).get(columnIndex);
            }
        };
        table.setModel(tableModel);
    }

    private void loadSalesData() {
        Connection conn = DButil.getConnection();
        String sql = "SELECT " +
                "  p.spid, " +
                "  p.spname, " +
                "  SUM(pi.spnumber) AS total_sales, " +
                "  SUM(pi.spprice * pi.spnumber) AS total_amount " +
                "FROM purchase_info pi " +
                "JOIN product_info p ON pi.spid = p.spid " +
                "GROUP BY p.spid, p.spname " +
                "ORDER BY total_sales DESC";

        try (Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Vector<Object> row = new Vector<>();
                row.add(rs.getInt("spid"));
                row.add(rs.getString("spname"));
                row.add(rs.getInt("total_sales"));
                row.add(rs.getDouble("total_amount"));
                vector.add(row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据加载失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            DButil.close(conn);
        }
    }


}
